package com.ssi.mrms.action.result;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;

public class BytesResult implements Result {

	private static final long serialVersionUID = -4415527426021564409L;

	public void execute(ActionInvocation invocation) throws Exception {
		IResultType action = (IResultType) invocation.getAction();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType(action.getContentType());
		Map<String, String> headers = action.getHeader();
		if (headers != null) {
			Iterator<String> iterator = headers.keySet().iterator();
			while (iterator.hasNext()) {
				String key = iterator.next();
				response.setHeader(key, headers.get(key));
			}
		}
		response.getOutputStream().write(action.getBytes());
		response.getOutputStream().flush();
	}
}